/*
 * Copyright (C) 2009 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.collections;

import java.util.*;

/**
 * Contains utility methods for manipulating with lists.
 */
public class Lists {

    private Lists() {}

    /**
     * @param it elements to be contained in the new list, in order
     * @param <E> type of elements
     * @return a new array list containing the given elements
     * @throws NullPointerException it is null
     */
    public static <E> ArrayList<E> newArrayList(Iterator<? extends E> it) {
        ArrayList<E> list = new ArrayList<E>();
        while(it.hasNext()) {
            list.add(it.next());
        }
        return list;
    }

    /**
     * @param collection to be contained in the new list, in order
     * @param <E> type of elements
     * @return a new array list containing the given elements
     * @throws NullPointerException collection is null
     */
    public static <E> ArrayList<E> newArrayList(Collection<? extends E> collection) {
        return new ArrayList<E>(collection);
    }

    /**
     * @param <E> type of elements
     * @return a new arraylist with default capacity
     */
    public static <E> ArrayList<E> newArrayList() {
        return new ArrayList<E>();
    }

    /**
     * @param t elements to be added to a new list
     * @param <E> type of elements
     * @return a new arraylist with capacity to fit the specified elements, containing all the
     * provided elements in order
     */
    public static <E> ArrayList<E> newArrayList(E... t) {
        return new ArrayList<E>(Arrays.asList(t));
    }
}
